package com.no1.musicmgr.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.no1.musicmgr.entity.Comments;
import com.no1.musicmgr.entity.User;
import com.no1.musicmgr.service.CommentsService;
import com.no1.musicmgr.vo.PageQueryBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@Controller
@Log4j
public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    @Operation(description = "新增评论")
    @Parameter(name = "comments", description = "评论对象")
    @PostMapping("/comments")
    public boolean addComments(Comments comments) {
        log.info("新增的comments:" + comments);
        return commentsService.addComments(comments);
    }

    @Operation(description = "删除评论")
    @Parameter(name = "commentsId", description = "评论ID")
    @DeleteMapping("/comments/{commentsId}")
    public boolean deleteComments(@PathVariable("commentsId")Integer commentsId) {

        return commentsService.deleteComments(commentsId);
    }

    @Operation(summary = "查询评论")
    @GetMapping("/comments/{commentsId}")
    @Parameter(name = "commentsId", description = "评论ID")
    public Comments selectYhm(@PathVariable("commentsId") Integer commentsId) {
        return commentsService.selectById(commentsId);
    }



    @Operation(description = "修改评论")
    @Parameter(name = "comments", description = "评论对象")
    @PutMapping("/comments")
    public boolean updateComments(Comments comments) {

        log.info("修改前的comments:" + comments);
        return commentsService.updateComments(comments);
    }

    @Operation(description = "查询评论(多)")
    @Parameter(name = "comments", description = "评论对象")
    @Parameter(name = "page", description = "分页对象")
    @GetMapping("/comments/list")
    public PageInfo<Comments> listComments(Comments comments, PageQueryBean page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        log.info("拿到的comments:" + comments);
        ArrayList<Comments> list = commentsService.listComments(comments);
        log.info("查询到的数据：" + list);
        return new PageInfo<Comments>(list);


    }

    @Operation(description = "查询评论(单)")
    @Parameter(name = "comments", description = "评论对象")
    @GetMapping("/comments/select")
    public Comments selectComments(Comments comments) {
        return commentsService.selectComments(comments);
    }

    @Operation(description = "歌曲界面展示评论")
    @Parameter(name = "comments", description = "评论对象")
    @GetMapping("/comments/list1")
    public ArrayList<Comments> listComments1(Comments comments) {
        comments.setComments_state(1);
        return commentsService.listComments(comments);
    }
}
